package linuxbox.bbs.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.bbs.logic.*;

/**
 * This class displays one bbs topic.
 */
public class ShowTopic extends TagSupport {
  long topicID = -1;
  int catID = -1;
  long msgID = -1;

  public int doStartTag() throws JspException {
    try {
      JspWriter out = pageContext.getOut();

      BbsManager bm = BbsManager.getInstance();
      Vector v = bm.getTopic(topicID, catID);

      int length = v.size();
      for (int j = 0; j < length; j++) {
        BbsMsg msg = (BbsMsg)v.elementAt(j);
        out.print("<a href=\"");
        out.print(BoxStatic.servletDir);
        out.print("BbsViewAction?CAT_ID=" + catID +
                  "&MSG_ID=" + msg.getID() + "&TOPIC_ID=" + msg.getTopicID() + "\">");

        String IndentSpace = "";
        for (int k = 0; k < msg.getIndLvl(); k++)
          IndentSpace = IndentSpace + "&nbsp;&nbsp;&nbsp;&nbsp;";

        String subject = msg.getSubject();
        if (subject.equals("")) {
          subject = "(No Subject)";
        }

        if (msg.getID() == msgID) {
          out.print("<b>" + IndentSpace + subject + " [ " + msg.getAuthor() + ", "
                    + msg.getSubDate().toString() + " " + msg.getSubTime().toString() + " ]</b>");
        } else {
          out.print(IndentSpace + subject + " [ " + msg.getAuthor() + ", "
                    + msg.getSubDate().toString() + " " + msg.getSubTime().toString() + " ]");
        }

        out.println("</a><br>");
      }
      out.println("<p>");
      out.flush();
      //out.close();
    } catch(Exception e) {
      BoxStatic.logger.debug("Error : " + e.getMessage());
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setTopicID(String aValue) {
    topicID = Long.parseLong(aValue);
  }

  public void setMsgID(String aValue) {
    msgID = Long.parseLong(aValue);
  }

  public void setCatID(String aValue) {
    catID = Integer.parseInt(aValue);
  }
}